package client;

import java.net.*;
import java.io.*;

public class ChatClientThread extends Thread {
	private Socket socket = null;
	private ChatClient client = null;
	private DataInputStream streamIn = null;
	private ChatFenster chatFenster;

	public ChatClientThread(ChatClient client, Socket socket,
			ChatFenster chatFenster) {
		this.client = client;
		this.socket = socket;
		this.chatFenster = chatFenster;
		open();
		start();
	}

	public void open() {
		try {
			streamIn = new DataInputStream(socket.getInputStream());
		} catch (IOException ioe) {
			chatFenster.appendToChat("Fehler bei Input " + ioe);
			client.stop();
		}
	}

	public void close() {
		try {
			if (streamIn != null)
				streamIn.close();
		} catch (IOException ioe) {
			chatFenster.appendToChat("Schlie\u00DFe Eingabe,.... " + ioe);
		}
	}

	public void run() {
		while (true) {
			try {
				client.handle(streamIn.readUTF());
			} catch (IOException ioe) {
				// chatFenster.appendToChat("\nEmpfang: " + ioe.getMessage());
				try {
					streamIn.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
				client.stop();
				chatFenster.dispose();
			}
		}
	}
}
